<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace gdbase\taglib;

use think\template\TagLib;

/**
 * CX标签库解析类
 * @category   Think
 * @package  Think
 * @subpackage  Driver.Taglib
 * @author    liu21st <liu21st@gmail.com>
 */
class Model extends Taglib
{

    // 标签定义
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'safe_show'        => ['attr' => 'model,field,default', 'close' => 0],
        'safe_if'         => ['attr' => 'condition', 'expression' => true],
        'safe_elseif'     => ['attr' => 'condition', 'close' => 0, 'expression' => true],
    ];

    /**
     * 安全显示model内的列，一般用于表单 添加 和编辑复用的表单
     * 格式：
     * {model:safe_show model="model" field="test" /}
     * {model:safe_show field="test" /}
     * @access public
     * @param  array $tag 标签属性 model  可空 默认 model  ,field,default
     * @param  string $content 标签内容 无
     * @return string
     */
    public function tagSafe_show($tag, $content)
    {
        if(empty($tag['model'])) $tag['model'] = 'model';
        if(!array_key_exists('default',$tag)) $tag['default'] = '';
        $parseStr = '<?php if(empty($'.$tag['model'].')){echo (\''.$tag['default'].'\');}elseif(empty($'.$tag['model'].'[\''.$tag['field'].'\'])){echo (\''.$tag['default'].'\');}else{echo ($'.$tag['model'].'[\''.$tag['field'].'\']);} ?>';

        return $parseStr;
    }

    /**
     * if标签解析
     * 格式：
     * {if condition=" $a eq 1"}
     * {elseif condition="$a eq 2" /}
     * {else /}
     * {/if}
     * 表达式支持 eq neq gt egt lt elt == > >= < <= or and || &&
     * @access public
     * @param  array $tag 标签属性
     * @param  string $content 标签内容
     * @return string
     */
    public function tagSafe_if($tag, $content)
    {
        $condition = !empty($tag['expression']) ? $tag['expression'] : $tag['condition'];
        $condition = $this->parseCondition($condition);
        $parseStr  = '<?php if(' . $condition . '): ?>' . $content . '<?php endif; ?>';

        return $parseStr;
    }

    /**
     * elseif标签解析
     * 格式：见if标签
     * @access public
     * @param  array $tag 标签属性
     * @param  string $content 标签内容
     * @return string
     */
    public function tagElseif($tag, $content)
    {
        $condition = !empty($tag['expression']) ? $tag['expression'] : $tag['condition'];
        $condition = $this->parseCondition($condition);
        $parseStr  = '<?php elseif(' . $condition . '): ?>';

        return $parseStr;
    }

    /**
     * else标签解析
     * 格式：见if标签
     * @access public
     * @param  array $tag 标签属性
     * @return string
     */
    public function tagElse($tag)
    {
        $parseStr = '<?php else: ?>';

        return $parseStr;
    }
}
